package xyz.lne.researchgroupmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import xyz.lne.researchgroupmanage.entity.*;
import xyz.lne.researchgroupmanage.exception.BadRequestException;
import xyz.lne.researchgroupmanage.mapper.*;
import xyz.lne.researchgroupmanage.service.KeywordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.lne.researchgroupmanage.service.dataTransferObject.KeyWordDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author team7
 * @since 2021-01-19
 */
@Service
public class KeywordServiceImpl extends ServiceImpl<KeywordMapper, Keyword> implements KeywordService {


    @Autowired
    private ResearchgroupMapper researchgroupMapper;

    @Autowired
    private ResearchMapper researchMapper;
    @Autowired
    private KeywordResearchgroupMapper keywordResearchgroupMapper;

    @Autowired
    private KeywordResearchMapper keywordResearchMapper;

    @Autowired
    private KeywordMapper keywordMapper;

    @Override
    @Transactional
    public List<KeyWordDTO> findKeywordByResearchGroupId(Integer researchGroupId) {

        //TODO：由于researchGroup实体类在被mybatisPlus转化数据库表时映射错误，导致本段代码无法使用
/*        QueryWrapper<ResearchGroup> RgWrapper = new QueryWrapper<>();
        if (researchgroupMapper.selectCount(RgWrapper.eq("researchgroup_id", researchGroupId)) == 0) {
            Map<String, Object> e = new HashMap<>();
            e.put("课题组不存在", researchGroupId);
            throw new BadRequestException(e);
        }*/

        List<KeyWordDTO> keyWordDTOList;
        List<Integer> keyWordIdList;

        QueryWrapper<KeywordResearchgroup> wrapper = new QueryWrapper<>();
        wrapper.eq("researchgroup_id", researchGroupId);
        keyWordIdList = keywordResearchgroupMapper.selectObjs(wrapper)
                .stream().map(Integer.class::cast)
                .collect(Collectors.toList());

        if(keyWordIdList.isEmpty()){
            return new ArrayList<>();
        }

        //TODO: 如果效率存在瓶颈，可以更改实现方式为用注解写sql
        keyWordDTOList = keywordMapper.selectBatchIds(keyWordIdList).stream()
                .map(Keyword::convertToDTO)
                .collect(Collectors.toList());
        return keyWordDTOList;
    }

    @Override
    @Transactional
    public List<KeyWordDTO> findKeywordByResearchId(Integer researchId) {
        QueryWrapper<Research> RWrapper=new QueryWrapper<>();
        if(researchMapper.selectCount(RWrapper.eq("research_id",researchId))==0){
            Map<String, Object> e = new HashMap<>();
            e.put("课题不存在", researchId);
            throw new BadRequestException(e);
        }

        List<KeyWordDTO> keyWordDTOList=new ArrayList<>();
        List<Integer> keyWordIdList=new ArrayList<>();

        QueryWrapper<KeywordResearch> wrapper=new QueryWrapper<>();
        wrapper.eq("research_id",researchId);
        keyWordIdList=keywordResearchMapper.selectObjs(wrapper)
                .stream().map(Integer.class::cast)
                .collect(Collectors.toList());

        if(keyWordIdList.isEmpty()){
            return new ArrayList<>();
        }

        keyWordDTOList=keywordMapper.selectBatchIds(keyWordIdList)
                .stream().map(Keyword::convertToDTO)
                .collect(Collectors.toList());
        return keyWordDTOList;
    }

    @Override
    @Transactional
    public List<KeyWordDTO> findKeyword(String keyword) {


        if(keyword.length()>30){
            Map<String, Object> e = new HashMap<>();
            e.put("关键词长度限制为30个字符", keyword);
            throw new BadRequestException(e);
        }

        QueryWrapper<Keyword> wrapper=new QueryWrapper<>();

        wrapper.eq("word",keyword);
        Keyword keywordInsert=new Keyword(null,keyword);
        if(keywordMapper.selectCount(wrapper)==0){
            keywordMapper.insert(keywordInsert);
            //TODO:addKeywordNode(Integer keywordId, String word) Neo4j功能加入
            //addKeywordNode(keywordInsert.getKeywordId(),KeywordInsert.getWord());
        }

        wrapper.clear();
        wrapper.like("word",keyword);
        return (keywordMapper.selectList(wrapper)).stream().map(Keyword::convertToDTO).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public List<KeyWordDTO> getDefaultKeywordList() {
        QueryWrapper<Keyword> kWrapper=new QueryWrapper<>();
        kWrapper.lt("keyword_id",6);

        return keywordMapper.selectList(kWrapper).stream().map(Keyword::convertToDTO).collect(Collectors.toList());
    }

}
